package com.ZeroCarbon.service.impl;

import cn.hutool.core.util.StrUtil;
import com.ZeroCarbon.domain.po.Avatar;
import com.ZeroCarbon.domain.po.BalanceIntegral;
import com.ZeroCarbon.domain.po.UserBaseInfo;
import com.ZeroCarbon.exception.BusinessLogicException;
import com.ZeroCarbon.mapper.BaseInfoMapper;
import com.ZeroCarbon.service.IBalanceIntegralService;
import com.ZeroCarbon.service.IBaseInfoService;
import com.ZeroCarbon.utils.UserHolder;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.ZeroCarbon.exception.BusinessLogicException.ERROR_EXCEPTION;

/**
 * 个人信息实现类
 *
 * @since 2024-10-8
 */
@Service
@RequiredArgsConstructor
public class BaseInfoServiceImpl extends ServiceImpl<BaseInfoMapper, UserBaseInfo> implements IBaseInfoService {

    private final IBalanceIntegralService balanceIntegralService;

    /**
     * 保存用户信息
     *
     * @param userBaseInfo 用户基本信息
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveBaseInfo(UserBaseInfo userBaseInfo) throws BusinessLogicException {
        //文件基路径
        String path = System.getProperty("user.dir").replace("\\", "/")
                + "/common/files/avatar/";
        //获取用户id
        String userId = userBaseInfo.getId();
        try {
            //保存用户头像
            boolean saveAvatar =
                    Db.save(new Avatar(userId, userBaseInfo.isGender() ? path + "boy.png" : path + "girl.png"));
            //没有保存头像成功则抛出异常
            if (!saveAvatar) {
                throw new BusinessLogicException();
            }
            //保存用户余额
            boolean saveBalanceIntegral =
                    Db.save(new BalanceIntegral(userId, 0, 0));
            //没有保存账户余额成功则抛出异常
            if (!saveBalanceIntegral) {
                throw new BusinessLogicException();
            }
        } catch (Exception e) {
            throw new BusinessLogicException(ERROR_EXCEPTION);
        }
        //保存用户信息
        if (!save(userBaseInfo)) {
            throw new BusinessLogicException(ERROR_EXCEPTION);
        }
    }

    /**
     * 获取用户信息
     *
     * @param userId 用户Id
     */
    @Override
    public UserBaseInfo getBaseInfo(String userId) throws BusinessLogicException {
        //获取用户id
        String id = UserHolder.getUser();
        //如果用户id为空，则采用传入的userId
        if (id == null) {
            id = userId;
        }
        //未登录也未传值的情况
        if (StrUtil.isBlank(id)) {
            return null;
        }
        //记录用户信息
        UserBaseInfo userInfo;
        try {
            //根据id获取用户信息
            userInfo = getById(id);
        } catch (Exception e) {
            throw new BusinessLogicException(ERROR_EXCEPTION);
        }
        //如果用户信息为空则返回空对象
        if (userInfo == null) {
            return new UserBaseInfo();
        }
        //获取账户余额
        BalanceIntegral balanceIntegral = balanceIntegralService.getBalanceIntegral();
        //设置用户余额
        userInfo.setBalanceIntegral(balanceIntegral);
        //返回用户信息
        return userInfo;
    }

    /**
     * 更新用户信息
     *
     * @param userBaseInfo 用户信息
     */
    @Override
    public void updateUser(UserBaseInfo userBaseInfo) throws BusinessLogicException {
        try {
            //更新用户信息
            if (!updateById(userBaseInfo)) {
                throw new BusinessLogicException();
            }
        } catch (Exception e) {
            throw new BusinessLogicException(ERROR_EXCEPTION);
        }
    }

    /**
     * 获取用户名
     *
     * @param userId 用户id
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String getUsername(String userId) throws BusinessLogicException {
        //用户名构造器
        LambdaQueryWrapper<UserBaseInfo> nameWrapper = new LambdaQueryWrapper<UserBaseInfo>()
                .select(UserBaseInfo::getName).eq(UserBaseInfo::getId, userId);
        try {
            //查找用户名
            return getOne(nameWrapper).getName();
        } catch (Exception e) {
            throw new BusinessLogicException(ERROR_EXCEPTION);
        }
    }
}
